# -*- coding: utf-8 -*-
import time
import requests
import ddddocr
import json


def get_sessionid():
    headers = {
        "authority": "data-backend.f6car.cn",
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "cache-control": "no-cache",
        "channel": "",
        "origin": "https://shop.f6car.cn",
        "pragma": "no-cache",
        "referer": "https://shop.f6car.cn/",
        "sec-ch-ua": "\"Microsoft Edge\";v=\"119\", \"Chromium\";v=\"119\", \"Not?A_Brand\";v=\"24\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-site",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0",
        "x-requested-with": "XMLHttpRequest"
    }
    cookies = {
        "sajssdk_2015_cross_new_user": "1",
        "sensorsdata2015jssdkcross": "%7B%22distinct_id%22%3A%2218c0693c70f904-0f68c9b5b17ab28-4c657b58-2073600-18c0693c71018b%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22%24device_id%22%3A%2218c0693c70f904-0f68c9b5b17ab28-4c657b58-2073600-18c0693c71018b%22%7D",
        "erpLanguage": "zh-CN"
    }
    url = "https://data-backend.f6car.cn/f6-data-backend/data/login/index"
    params = {
        "channel": "f6wx",
        "autoLoginToken": "",
        "token": ""
    }
    response = requests.get(url, headers=headers, cookies=cookies, params=params)
    backendSESSIONID = response.cookies['f6-data-backendSESSIONID']
    print(f"获取到的ck为:{backendSESSIONID}")
    return backendSESSIONID


def get_img(backendSESSIONID):
    headers = {
        "authority": "data-backend.f6car.cn",
        "accept": "image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "cache-control": "no-cache",
        "pragma": "no-cache",
        "referer": "https://shop.f6car.cn/",
        "sec-ch-ua": "\"Microsoft Edge\";v=\"119\", \"Chromium\";v=\"119\", \"Not?A_Brand\";v=\"24\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "image",
        "sec-fetch-mode": "no-cors",
        "sec-fetch-site": "same-site",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
    }
    cookies = {
        "f6-data-backendSESSIONID": backendSESSIONID,
        "sensorsdata2015jssdkcross": "%7B%22distinct_id%22%3A%2218bdcaff345aa7-00480f2b9d6480f4-4c657b58-2073600-18bdcaff346bdb%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22%24device_id%22%3A%2218bdcaff345aa7-00480f2b9d6480f4-4c657b58-2073600-18bdcaff346bdb%22%7D",
        "erpLanguage": "zh-CN"
    }
    url = "https://data-backend.f6car.cn/f6-data-backend/data/user/captcha-image"
    params = {
        f"{int(time.time())}": ""
    }
    response = requests.get(url, headers=headers, cookies=cookies, params=params)
    with open('captcha.png', 'wb') as w:
        w.write(response.content)


def identify_img():
    ocr = ddddocr.DdddOcr()
    with open('captcha.png', 'rb') as f:
        img_bytes = f.read()
    res = ocr.classification(img_bytes)
    print('识别出的验证码为：' + res)
    return res


def get_phone_info(backendSESSIONID, identify, phoneNo):
    headers = {
        "authority": "data-backend.f6car.cn",
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "cache-control": "no-cache",
        "channel": "1",
        "content-type": "application/json;charset=UTF-8",
        "origin": "https://shop.f6car.cn",
        "pragma": "no-cache",
        "referer": "https://shop.f6car.cn/",
        "sec-ch-ua": "\"Microsoft Edge\";v=\"119\", \"Chromium\";v=\"119\", \"Not?A_Brand\";v=\"24\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-site",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0",
        "x-requested-with": "XMLHttpRequest"
    }
    cookies = {
        "f6-data-backendSESSIONID": backendSESSIONID,
        "sajssdk_2015_cross_new_user": "1",
        "sensorsdata2015jssdkcross": "%7B%22distinct_id%22%3A%2218c0693c70f904-0f68c9b5b17ab28-4c657b58-2073600-18c0693c71018b%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22%24device_id%22%3A%2218c0693c70f904-0f68c9b5b17ab28-4c657b58-2073600-18c0693c71018b%22%7D",
        "erpLanguage": "zh-CN"
    }
    url = "https://data-backend.f6car.cn/f6-data-backend/data/user/sendCodeMsg"
    data = {
        "phoneNo": phoneNo,
        "jCode": identify,
        "channelId": 1
    }
    data = json.dumps(data, separators=(',', ':'))
    response = requests.post(url, headers=headers, cookies=cookies, data=data)
    print(response.text)


def verify(backendSESSIONID, phoneNo, randomCode):
    headers = {
        "authority": "data-backend.f6car.cn",
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "cache-control": "no-cache",
        "channel": "1",
        "origin": "https://shop.f6car.cn",
        "pragma": "no-cache",
        "referer": "https://shop.f6car.cn/",
        "sec-ch-ua": "\"Microsoft Edge\";v=\"119\", \"Chromium\";v=\"119\", \"Not?A_Brand\";v=\"24\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-site",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0",
        "x-requested-with": "XMLHttpRequest"
    }
    cookies = {
        "f6-data-backendSESSIONID": backendSESSIONID,
        "sajssdk_2015_cross_new_user": "1",
        "sensorsdata2015jssdkcross": "%7B%22distinct_id%22%3A%2218c0693c70f904-0f68c9b5b17ab28-4c657b58-2073600-18c0693c71018b%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22%24device_id%22%3A%2218c0693c70f904-0f68c9b5b17ab28-4c657b58-2073600-18c0693c71018b%22%7D",
        "erpLanguage": "zh-CN"
    }
    url = "https://data-backend.f6car.cn/f6-data-backend/data/login/broswer"
    params = {
        "tel": phoneNo,
        "randomCode": randomCode
    }
    response = requests.get(url, headers=headers, cookies=cookies, params=params)

    print(response.text)
    # print(response)
    print(f'通过验证的session:{backendSESSIONID}')


def main():
    backendSESSIONID = get_sessionid()
    get_img(backendSESSIONID)
    identify = identify_img()
    phoneNo = input('输入手机号:')
    get_phone_info(backendSESSIONID, identify, phoneNo)
    randomCode = input('输入验证码：')
    verify(backendSESSIONID, phoneNo, randomCode)


if __name__ == '__main__':
    main()


# 1204
# 13264722119
# 13228683025
# 13243531798
# 13257909150
# 13290078275
# 13247169335
# 13220359289
# 13272707178
# 13213846935
# 13290022476
# 13237547205
# 13243537202
# 13220355838
# 13257909063
# 13271879783
#
# 1205
# 15679043582 72e9a2bf-36fe-4061-8676-6788d7d73a88
# 15649463759
# 15649463082
# 15664235097
# 15649463075
# 13137751743
# 13194045076 f9497675-c9ec-4e90-8e7f-4d047c6a0a57
# 13103657024
#
# 1206
# 13183342439
# 13183339542
# 13103758481 90ebdce8-34ef-4d79-a7c6-025a3c76542a
# 13137501948 f0bb0f90-17de-4533-9c02-e4cbf92740e2
# 13194044793
# 13137524917
# 19231060102 f550b927-0026-4175-82b9-07b4e5fbef0c
